package org.springblade.tms.module.exceptiontype.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.tms.module.exceptionreason.entity.TMExceptionReason;
import org.springblade.tms.module.exceptionreason.service.IExceptionReasonService;
import org.springblade.tms.module.exceptionreason.wrapper.ExceptionReasonWrapper;
import org.springblade.tms.module.exceptiontype.dto.TMExceptionTypeDTO;
import org.springblade.tms.module.exceptiontype.entity.TMExceptionType;
import org.springblade.tms.module.exceptiontype.service.IExceptionTypeService;
import org.springblade.tms.module.exceptiontype.vo.TMExceptionTypeVO;
import org.springblade.tms.module.exceptiontype.wrapper.ExceptionTypeWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.exceptiontype.base
 * @className: ExceptionMoldService
 * @author: yr
 * @description: 异常类型服务类
 * @date: 2022/3/23 10:48
 */
@Service
@Transactional
public class ExceptionMoldService {
	@Resource
	private IExceptionTypeService exceptionTypeService;
	@Resource
	private IExceptionReasonService exceptionReasonService;

	/**
	 * @param tmExceptionType:
	 * @param query:
	 * @return R<IPage < TMExceptionTypeVO>>
	 * @author yr
	 * @description 异常类型查询
	 * @date 2022/3/23 15:59
	 */
	public R<IPage<TMExceptionTypeVO>> list(TMExceptionType tmExceptionType, Query query) {
		IPage<TMExceptionType> page = exceptionTypeService.page(Condition.getPage(query), Condition.getQueryWrapper(new TMExceptionType()).lambda()
			.like(tmExceptionType.getEventReasonCode() != null && !tmExceptionType.getEventReasonCode().isEmpty(),
				TMExceptionType::getEventReasonCode, tmExceptionType.getEventReasonCode())
			.like(tmExceptionType.getName() != null && !tmExceptionType.getName().isEmpty(),
				TMExceptionType::getName, tmExceptionType.getName()).orderByDesc(TMExceptionType::getCreateTime,TMExceptionType::getId));
		List<Long> ids = page.getRecords().stream().map(TMExceptionType::getId).collect(Collectors.toList());
		Map<Long, List<TMExceptionReason>> exceptionReasonMap = exceptionReasonService.lambdaQuery().in(ids != null && !ids.isEmpty(), TMExceptionReason::getParentId, ids).list()
			.stream().collect(Collectors.groupingBy(TMExceptionReason::getParentId));
		IPage<TMExceptionTypeVO> pageVO = ExceptionTypeWrapper.build().pageVO(page);
		for (TMExceptionTypeVO record : pageVO.getRecords()) {
			if (exceptionReasonMap != null && exceptionReasonMap.get(record.getId()) != null) {
				record.setExceptionReasonVO(ExceptionReasonWrapper.build().listVO(exceptionReasonMap.get(record.getId())));
			}
		}
		return R.data(pageVO);
	}

	/**
	 * @param exceptionTypeDTO:
	 * @return R
	 * @author yr
	 * @description 新增或修改异常类型
	 * @date 2022/3/23 16:08
	 */
	public R saveOrUpdate(TMExceptionTypeDTO exceptionTypeDTO) {
		//判断新增还是修改
		TMExceptionType tmExceptionType = BeanUtil.copy(exceptionTypeDTO, TMExceptionType.class);
		exceptionTypeService.saveOrUpdate(tmExceptionType);
		exceptionTypeDTO.getExceptionReason().forEach(tmExceptionReason -> tmExceptionReason.setParentId(tmExceptionType.getId()));
		List<TMExceptionReason> tmExceptionReasons = exceptionReasonService.lambdaQuery().eq(TMExceptionReason::getParentId, tmExceptionType.getId()).list();
		//删除部分
		tmExceptionReasons.removeAll(exceptionTypeDTO.getExceptionReason());
		//新增或修改部分
		exceptionTypeDTO.getExceptionReason().removeAll(tmExceptionReasons);
		List<Long> ids = tmExceptionReasons.stream().map(TMExceptionReason::getId).collect(Collectors.toList());
		if (!ids.isEmpty()) {
			exceptionReasonService.deleteLogic(ids);
		}
		exceptionReasonService.saveOrUpdateBatch(exceptionTypeDTO.getExceptionReason());
		return R.status(true);
	}

	/**
	 * @param ids:
	 * @return R
	 * @author yr
	 * @description 删除异常类型
	 * @date 2022/3/23 16:22
	 */
	public R remove(List<Long> ids) {
		List<Long> idList = exceptionReasonService.lambdaQuery().in(TMExceptionReason::getParentId, ids).list()
			.stream().map(TMExceptionReason::getId).collect(Collectors.toList());
		exceptionTypeService.deleteLogic(ids);
		if (!idList.isEmpty()){
			exceptionReasonService.deleteLogic(idList);
		}
		return R.status(true);
	}
}
